<?php

namespace App;

use App\BaseModel;
use Illuminate\Database\Eloquent\Builder;

class Post extends BaseModel
{
    // 关联用户
    public function user() {
        //return $this->belongsTo('App\User', 'user_id', 'id');  做遵循规则，可简写后两个参数
        return $this->belongsTo('App\User');
    }

    // 评论模型  根据创建时间排序
    public function comments() {
        return $this->hasMany('App\Comment')->orderBy('created_at', 'desc');
    }

    // 查询 一篇文章相对某个用户ID 是否有赞
    public function zan($user_id) {
        return $this->hasOne(\App\Zan::class)->where('user_id', $user_id);
    }

    // 文章的所有赞
    public function zans() {
        return $this->hasMany(\App\Zan::class);
    }

    // 属于某个作者的文章
    public function scopeAuthorBy(Builder $query, $user_id) {
        return $query->where('user_id', $user_id);
    }

    public function postTopics() {
        return $this->hasMany(\App\PostTopic::class, 'post_id', 'id');
    }

    // 不属于某个专题的文章
    public function scopeTopicNotBy(Builder $query, $topic_id) {
        return $query->doesntHave('postTopics', 'and', function($q) use($topic_id) {
            $q->where('topic_id', $topic_id);
        });
    }

}
